<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
        <el-button
          type="primary"
          size="small"
          plain
          @click="handleImport">抽取专家
        </el-button>
        <el-button
          type="primary"
          size="small"
          plain
          @click="handleHistory">历史记录
        </el-button>
      </template>
    </avue-crud>

    <el-dialog
      title="抽取专家"
      append-to-body
      :visible.sync="excelBox"
      width="600"
      class="crud-container">
      <avue-form
        :option="optionEvent"
        v-model="excelForm"
        :upload-after="uploadAfter">
      </avue-form>
      <div style="display: flex; justify-content: flex-end;">
        <el-button
          type="primary"
          size="small"
          plain
          icon="el-icon-refresh"
          @click="extract">抽取专家
        </el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="历史记录"
      append-to-body
      :visible.sync="historyBox"
      width="600"
      class="crud-container">
      <avue-crud
        :option="optionEvent"
        ref="crudDialog"
        :data="historyData"
        @search-change="searchChange"
        :page.sync="pageSub"
        :permission="historyPermissionList"
        @current-change="currentChangeSub"

        @size-change="sizeChangeSub"
        @refresh-change="refreshChangeSub"
      >
        <template slot-scope="scope" slot="menu">
          <el-button type="text" size="small"  @click.stop="showExpertList(scope.row)">详情
          </el-button>
        </template>
      </avue-crud>
    </el-dialog>
    <el-dialog title="专家信息列表" :visible.sync="dialogVisible" append-to-body height="500px">
      <avue-crud :data="detailData" :option="detailOption" />
    </el-dialog>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove, save, getHistoryList,getExpertList} from "@/api/business/expert";
import option from "@/const/business/expert";
import {mapGetters} from "vuex";
import optionEventExpert from "@/const/business/eventExpert"
import detailOption from "@/const/business/detailOption"
export default {
  data() {
    return {
      dialogVisible:false,
      pageSub: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      historyData: [],
      excelForm: {},
      EventExpertForm: {},
      excelBox: false,
      historyBox: false,
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: option,
      detailOption: detailOption,
      optionEvent: {
        menuWidth: 100,
        refreshBtn: false,
        columnBtn: false,
        submitBtn: false,
        emptyBtn: false,
        height: '380px',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: false,
        viewBtn: true,
        selection: false,
        dialogClickModal: false,
        dialogMenuPosition: 'right',
        saveBtn: false,
        cancelBtn: false,
        column: [
          {
            label: "事件标题",
            prop: "title",
            type: "input",
            span: 24,
          },
          {
            label: "医院等级",
            prop: "hospitalGrade",
            type: "select",
            dicData: [
              {
                label: "一级",
                value: '一级'
              },
              {
                label: "二级",
                value: "二级"
              }, {
                label: "三级",
                value: "三级"
              }
            ],
          },
          {
            label: "专家数量",
            prop: "amount",
            type: 'number',
            rules: [{
              required: false,
              message: "请输入整数",
              trigger: "blur",
              pattern: /^(?:[1-9]|[1-4][0-9]|50)$/,
            }],
          },
          {
            label: "专业领域",
            prop: "specialty",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=specialty",
            //dataType: "number",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
          },
          {
            label: "专家数量",
            prop: "amount1",
            type: 'number',
            hide: true,
            rules: [{
              required: false,
              message: "请输入整数",
              trigger: "blur",
              pattern: /^[0-50]?$/,
            }],
          },
        ]
      },
      optionEventExpert: optionEventExpert,
      data: [],
      detailData: [],
      eventData: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.expert_add, true),
        viewBtn: this.vaildData(this.permission.expert_view, true),
        delBtn: this.vaildData(this.permission.expert_delete, true),
        editBtn: this.vaildData(this.permission.expert_edit, true)
      };
    },
    historyPermissionList() {
      return {
        addBtn: this.vaildData(this.permission.expert_view, false),
        viewBtn: this.vaildData(this.permission.expert_view, false),
        delBtn: this.vaildData(this.permission.expert_delete, false),
        editBtn: this.vaildData(this.permission.expert_delete, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    //详细信息
    showExpertList(row) {
      // 调用后端接口，将选中行的数据传递给后端进行查询
      getExpertList(row.id).then(res => {
        // 将返回的专家信息列表保存到 data 中，然后在弹窗中使用该数据展示
        //console.log(res)
        this.detailData =  res.data.data;
        this.dialogVisible = true; // 显示弹窗
      });
    },
    handleHistory() {
      this.historyBox = true; // 打开历史记录弹窗
      this.onLoadHistory(); // 加载历史记录数据
    },
    onLoadHistory() {
      this.loading = true;
      // 调用获取历史记录数据的接口，并传入分页参数和搜索条件
      getHistoryList(this.pageSub.currentPage, this.pageSub.pageSize, this.query).then(res => {
        const data = res.data.data;
        this.pageSub.total = data.total;
        this.historyData = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crudDialog.$nextTick(() => {
            const avueCrud = this.$refs.crudDialog.$refs.crud;
            if (avueCrud && typeof avueCrud.loadData === 'function') {
              avueCrud.loadData(this.historyData);
            }
          });
        });
      });
    },

    uploadAfter(res, done, loading, column) {
      this.excelForm = {};
      //console.log(res.length);
      // this.excelBox = false;
      this.refreshChange();
      done();
    },
    handleImport() {
      //this.excelForm = null;
      this.excelBox = true;
    },
    extract() {
      //console.log(this.excelForm)
      save(this.excelForm).then(() => {
        //this.excelForm=null;
        //debugger
        this.$message({
          type: "success",
          message: "抽取成功"
        });
      }, error => {
        //loading();
        window.console.log(error);
      });
      // this.excelForm={};
    },

    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        console.log(error);
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    currentChangeSub(currentPage) {
      this.pageSub.currentPage = currentPage;
      this.onLoadHistory(this.pageSub, this.query);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    sizeChangeSub(pageSize) {
      this.pageSub.pageSize = pageSize;
      this.onLoadHistory(this.pageSub, this.query);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    refreshChangeSub() {
      this.onLoadHistory(this.pageSub, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
